package cn.cmkj.auction.data.http;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cn.cmkj.auction.app.Urls;

/**
 * Created by cunguoyao on 2017/9/24.
 */

public class ShuhuaOrder implements Serializable {

    private String orderId;
    private List<Shuhua> shuhuaList;
    private String orderCode;
    private String orderTime;
    private String payTime;
    private String finishTime;
    private Address orderAddress;
    private Shipping shipping;//后面再改为List
    private Payment payment;//后面再改为List
    private int closePayTimes;

    private BigDecimal totalGoodsPrice;
    private BigDecimal totalDiscount;
    private BigDecimal totalShouldPay;
    private String payMethod;
    private String orderStatus;//
    private String payStatusStr;
    private String shipStatusStr;
    private String displayStatusText;

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public List<Shuhua> getShuhuaList() {
        return shuhuaList;
    }

    public void setShuhuaList(List<Shuhua> shuhuaList) {
        this.shuhuaList = shuhuaList;
    }

    public String getOrderCode() {
        return orderCode;
    }

    public void setOrderCode(String orderCode) {
        this.orderCode = orderCode;
    }

    public String getOrderTime() {
        return orderTime;
    }

    public void setOrderTime(String orderTime) {
        this.orderTime = orderTime;
    }

    public String getPayTime() {
        return payTime;
    }

    public void setPayTime(String payTime) {
        this.payTime = payTime;
    }

    public String getFinishTime() {
        return finishTime;
    }

    public void setFinishTime(String finishTime) {
        this.finishTime = finishTime;
    }

    public Address getOrderAddress() {
        return orderAddress;
    }

    public void setOrderAddress(Address orderAddress) {
        this.orderAddress = orderAddress;
    }

    public Shipping getShipping() {
        return shipping;
    }

    public void setShipping(Shipping shipping) {
        this.shipping = shipping;
    }

    public int getClosePayTimes() {
        return closePayTimes;
    }

    public void setClosePayTimes(int closePayTimes) {
        this.closePayTimes = closePayTimes;
    }

    public Payment getPayment() {
        return payment;
    }

    public void setPayment(Payment payment) {
        this.payment = payment;
    }

    public BigDecimal getTotalGoodsPrice() {
        return totalGoodsPrice;
    }

    public void setTotalGoodsPrice(BigDecimal totalGoodsPrice) {
        this.totalGoodsPrice = totalGoodsPrice;
    }

    public BigDecimal getTotalDiscount() {
        return totalDiscount;
    }

    public void setTotalDiscount(BigDecimal totalDiscount) {
        this.totalDiscount = totalDiscount;
    }

    public BigDecimal getTotalShouldPay() {
        return totalShouldPay;
    }

    public void setTotalShouldPay(BigDecimal totalShouldPay) {
        this.totalShouldPay = totalShouldPay;
    }

    public String getPayMethod() {
        return payMethod;
    }

    public void setPayMethod(String payMethod) {
        this.payMethod = payMethod;
    }

    public String getOrderStatus() {
        return orderStatus;
    }

    public void setOrderStatus(String orderStatus) {
        this.orderStatus = orderStatus;
    }

    public String getPayStatusStr() {
        return payStatusStr;
    }

    public void setPayStatusStr(String payStatusStr) {
        this.payStatusStr = payStatusStr;
    }

    public String getShipStatusStr() {
        return shipStatusStr;
    }

    public void setShipStatusStr(String shipStatusStr) {
        this.shipStatusStr = shipStatusStr;
    }

    public String getDisplayStatusText() {
        return displayStatusText;
    }

    public void setDisplayStatusText(String displayStatusText) {
        this.displayStatusText = displayStatusText;
    }

    //订单列表解析
    public static ShuhuaOrder parseFromJson(JSONObject json){
        ShuhuaOrder order = new ShuhuaOrder();
        if(json != null) {
            order.setOrderId(json.optString("order_id"));
            JSONArray itemList = json.optJSONArray("item");
            List<Shuhua> shuhuaList = new ArrayList<>();
            if(itemList != null && itemList.length() > 0) {
                for(int i=0;i<itemList.length();i++) {
                    JSONObject jsonObject = itemList.optJSONObject(i);
                    Shuhua shuhua = new Shuhua();
                    shuhua.setId(jsonObject.optLong("goods_id", 0));
                    shuhua.setProductId(jsonObject.optLong("product_id", 0));
                    shuhua.setName(jsonObject.optString("goods_name"));
                    shuhua.setQuantity(jsonObject.optInt("quantity"));
                    shuhua.setDefaultImage(ShuhuaImage.parseFromJson(jsonObject.optJSONObject("goods_pic"), false));
                    shuhuaList.add(shuhua);
                }
            }
            order.setShuhuaList(shuhuaList);
            double shouldPay = json.optDouble("amount", 0);
            order.setTotalShouldPay(BigDecimal.valueOf(shouldPay));
            order.setOrderTime(json.optString("createtime") + "000");
            order.setOrderStatus(json.optString("status", ""));
            order.setPayStatusStr(json.optString("pay_status", ""));
            order.setShipStatusStr(json.optString("ship_status", ""));
            order.setDisplayStatusText(json.optString("page_status", ""));
        }
        return order;
    }

    public static List<ShuhuaOrder> parseFromJsonArray(JSONArray array){
        List<ShuhuaOrder> list = new ArrayList<>();
        if(array != null && array.length() > 0){
            for(int i=0;i<array.length();i++){
                list.add(parseFromJson(array.optJSONObject(i)));
            }
        }
        return list;
    }

    //订单详情解析
    public static ShuhuaOrder parseFromJsonDetail(JSONObject jsonObject) {
        if(jsonObject != null) {
            ShuhuaOrder order = new ShuhuaOrder();
            order.setOrderId(jsonObject.optString("order_id"));
            List<Shuhua> list = new ArrayList<>();
            JSONObject jsonGoodsList = jsonObject.optJSONObject("goodsinfo").optJSONObject("goods");

            //通过迭代器获取这段json当中所有的key值
            Iterator keys = jsonGoodsList.keys();
            //然后通过一个循环取出所有的key值
            while (keys.hasNext()){
                String key = String.valueOf(keys.next());
                //最后就可以通过刚刚得到的key值去解析后面的json了
                String value = jsonGoodsList.optString(key, "");
                JSONObject jsonGoods = null;
                try {
                    jsonGoods = new JSONObject(value);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //商品列表
                Shuhua shuhua = Shuhua.parseFromJsonForOrder(jsonGoods);
                list.add(shuhua);
            }
            order.setShuhuaList(list);
            order.setOrderTime(jsonObject.optString("createtime") + "000");
            //订单地址
            Address address = Address.parseFromJson(jsonObject.optJSONObject("consignee"));
            order.setOrderAddress(address);
            //订单状态
            //支付状态
            order.setPayStatusStr(jsonObject.optString("payStatus", ""));
            //发货状态
            Shipping shipping = Shipping.parseFromJsonByOrderDetail(jsonObject.optJSONObject("shipping"));
            order.setShipping(shipping);
            order.setShipStatusStr(jsonObject.optString("shipStatus", ""));
            //总折扣
            order.setTotalDiscount(BigDecimal.valueOf(jsonObject.optJSONObject("total").optDouble("discountPrice", 0)));
            //总价
            order.setTotalShouldPay(BigDecimal.valueOf(jsonObject.optJSONObject("total").optDouble("total_amount")));
            return order;
        }
        return null;
    }

    //下单返回的订单对象解析
    public static ShuhuaOrder parseFromJsonByPreOrder(JSONObject json){
        ShuhuaOrder order = new ShuhuaOrder();
        Shipping shipping = Shipping.parseFromJsonByOrderCreate(json.optJSONArray("shipping").optJSONObject(0));
        order.setShipping(shipping);
        Payment payment = Payment.parseFromJson(json.optJSONArray("payment_list").optJSONObject(0));
        order.setPayment(payment);

        List<Shuhua> shuhuaList = new ArrayList<>();
        JSONArray itemList = json.optJSONObject("object").optJSONArray("goods");
        if(itemList != null && itemList.length() > 0) {
            for (int i = 0; i < itemList.length(); i++) {
                JSONObject jsonObject = itemList.optJSONObject(i);
                Shuhua shuhua = new Shuhua();
                shuhua.setId(jsonObject.optLong("goods_id", 0));
                shuhua.setProductId(jsonObject.optLong("product_id", 0));
                shuhua.setName(jsonObject.optString("name", ""));
                shuhua.setSalePrice(BigDecimal.valueOf(jsonObject.optDouble("total_price", 0)));
                ShuhuaImage image = new ShuhuaImage();
                String pic = jsonObject.optString("pic", "");
                if(pic.startsWith(Urls.web)) {
                    image.l_url = pic;
                    image.m_url = pic;
                    image.s_url = pic;
                }else {
                    image.l_url = Urls.web + "/" + pic;
                    image.m_url = Urls.web + "/" + pic;
                    image.s_url = Urls.web + "/" + pic;
                }
                shuhua.setDefaultImage(image);
                shuhuaList.add(shuhua);
            }
        }
        order.setShuhuaList(shuhuaList);

        order.setTotalGoodsPrice(BigDecimal.valueOf(json.optDouble("subtotal_goods_price", 0)));
        order.setTotalDiscount(BigDecimal.valueOf(json.optDouble("subtotal_discount_amount", 0)));
        order.setTotalShouldPay(BigDecimal.valueOf(json.optDouble("subtotal_price", 0)));
        return order;
    }
}
